/*
** task.c for Task in /home/vriely/Dropbox/work/Unix/Zappy_rendu/serv-src
**
** Made by Samuel Vrielynck
** Login   <vriely_s@epitech.net>
**
** Started on  Tue Jul 10 14:06:12 2012 Samuel Vrielynck
** Last update Fri Jul 20 09:33:41 2012 guillaume renard
*/

#include		<string.h>
#include		<stdlib.h>

#include		"server.h"

static char		*avance(t_client *, t_game *);
static char		*droite(t_client *, t_game *);
static char		*gauche(t_client *, t_game *);
static char		*voir(t_client *, t_game *);

static const t_game_fun	game_func[] =
{
  { "avance", &avance, 6 },
  { "droite", &droite, 6 },
  { "gauche", &gauche, 6 },
  { "voir", &voir, 4 },
  { "inventaire", &inventaire, 11 },
  { "fork", &pond, 4 },
  { "connect_nbr", &connect_nbr, 11 },
  { "prend", &prend, 5 },
  { "pose", &pose, 4 },
  { "broadcast", &broadcast, 9 },
  { "incantation", &incantation, 11 },
  { "expulse", &expulse, 7},
  { NULL, NULL, 0 }
};

static char		*voir(t_client *client, t_game *game)
{
  client->gamer->action = 7 * MILLI / game->time;
  return (look_player(game, client->gamer));
}

static char		*avance(t_client *client, t_game *game)
{
  client->gamer->action = 7  * MILLI / game->time;
  game->obj[client->gamer->coord.y][client->gamer->coord.x].inv[JOUEUR]--;
  move_player(game, client->gamer, client->gamer->direction);
  game->obj[client->gamer->coord.y][client->gamer->coord.x].inv[JOUEUR]++;
  if (game->graphic != NULL)
    serv_monitor("ppo", client, game, game->graphic);
  return (strdup("ok\n"));
}

static char		*droite(t_client *client, t_game *game)
{
  client->gamer->action = 7  * MILLI / game->time;
  client->gamer->direction >= LEFT ? client->gamer->direction = UP :
    client->gamer->direction++;
  if (game->graphic != NULL)
    serv_monitor("ppo", client, game, game->graphic);
  return (strdup("ok\n"));
}

static char		*gauche(t_client *client, t_game *game)
{
  client->gamer->action = 7  * MILLI / game->time;
  client->gamer->direction <= UP ? client->gamer->direction = LEFT :
    client->gamer->direction--;
  if (game->graphic != NULL)
    serv_monitor("ppo", client, game, game->graphic);
  return (strdup("ok\n"));
}

void			gere_task(t_client *client, t_game *game)
{
  char			*str;
  int			i;

  i = 0;
  str = (char *) client->gamer->task->head->content;
  while (game_func[i].func != NULL)
  {
    if (!strncmp(game_func[i].name, str, game_func[i].len))
    {
      client->gamer->action_str = game_func[i].func(client, game);
      break;
    }
    i++;
  }
  client->gamer->task->pop(client->gamer->task, NULL);
}
